package org.example.design.designmode.singleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: lingshi
 * @Date: 2020/5/10 19:13
 * @Description:
 */
public class ContainerSingleton {

    private static volatile Map <String,Object> inser = new ConcurrentHashMap<String,Object>(16);
    private ContainerSingleton(){}
    public static Object getInstance(String name) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
      //将当前map加锁
       synchronized (inser) {
           Object ob = null;
           if (!inser.containsKey(name)) {
               ob = Class.forName(name).newInstance();
               inser.put(name, ob);
               return inser;
           } else {
               return inser;
           }
       }
    }

}
